package salesSystem.Service;

import salesSystem.model.Order;
import salesSystem.model.OrderItem;

import java.util.ArrayList;
import java.util.List;

// 订单服务
public class OrderService {
    private InventoryService inventoryService;
    private PaymentService paymentService;
    private LogisticsService logisticsService;
    private List<Order> orderDatabase;

    public OrderService() {
        this.inventoryService = new InventoryService();
        this.paymentService = new PaymentService();
        this.logisticsService = new LogisticsService();
        this.orderDatabase = new ArrayList<>();
    }

    // 创建订单
    public Order createOrder(String customerId, List<OrderItem> items, String shippingAddress) {
        double totalAmount = calculateTotalAmount(items);
        Order order = new Order(customerId, items, totalAmount, shippingAddress);
        orderDatabase.add(order);
        System.out.println("订单创建成功: " + order.getOrderId());
        return order;
    }

    private double calculateTotalAmount(List<OrderItem> items) {
        return items.stream().mapToDouble(OrderItem::getItemTotal).sum();
    }

    // 审核订单
    public boolean approveOrder(String orderId) {
        Order order = findOrderById(orderId);
        if (order != null && order.getStatus().equals("CREATED")) {
            order.setStatus("APPROVED");
            System.out.println("订单审核通过: " + orderId);
            return true;
        }
        System.out.println("订单审核失败: " + orderId);
        return false;
    }

    // 处理支付
    public boolean processPayment(String orderId, String paymentMethod) {
        Order order = findOrderById(orderId);
        if (order != null && order.getStatus().equals("APPROVED")) {
            boolean paymentSuccess = paymentService.processPayment(order.getTotalAmount(), paymentMethod);
            if (paymentSuccess) {
                order.setStatus("PAID");
                order.setPaymentMethod(paymentMethod);
                System.out.println("支付处理成功: " + orderId);
                return true;
            }
        }
        System.out.println("支付处理失败: " + orderId);
        return false;
    }

    // 检查库存并分配
    public boolean prepareOrder(String orderId) {
        Order order = findOrderById(orderId);
        if (order != null && order.getStatus().equals("PAID")) {
            boolean inventoryAvailable = inventoryService.checkInventory(order.getItems());
            if (inventoryAvailable) {
                order.setStatus("PACKED");
                System.out.println("订单准备完成: " + orderId);
                return true;
            }
        }
        System.out.println("订单准备失败: " + orderId);
        return false;
    }

    // 发货
    public boolean shipOrder(String orderId) {
        Order order = findOrderById(orderId);
        if (order != null && order.getStatus().equals("PACKED")) {
            String trackingNumber = logisticsService.scheduleDelivery(
                    order.getOrderId(),
                    order.getShippingAddress(),
                    order.getItems()
            );
            order.setTrackingNumber(trackingNumber);
            order.setStatus("SHIPPED");
            System.out.println("订单已发货，物流单号: " + trackingNumber);
            return true;
        }
        System.out.println("发货失败: " + orderId);
        return false;
    }

    // 订单完成
    public boolean completeOrder(String orderId) {
        Order order = findOrderById(orderId);
        if (order != null && order.getStatus().equals("SHIPPED")) {
            order.setStatus("DELIVERED");
            System.out.println("订单已完成: " + orderId);
            return true;
        }
        return false;
    }

    // 取消订单
    public boolean cancelOrder(String orderId) {
        Order order = findOrderById(orderId);
        if (order != null && (order.getStatus().equals("CREATED") || order.getStatus().equals("APPROVED"))) {
            order.setStatus("CANCELLED");
            System.out.println("订单已取消: " + orderId);
            return true;
        }
        System.out.println("订单取消失败: " + orderId);
        return false;
    }

    // 查询订单
    public Order getOrder(String orderId) {
        return findOrderById(orderId);
    }

    // 私有方法：根据ID查找订单
    private Order findOrderById(String orderId) {
        return orderDatabase.stream()
                .filter(o -> o.getOrderId().equals(orderId))
                .findFirst()
                .orElse(null);
    }
}